package FactoryPattern;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Author: zhaojie
 * @Date: Created in 15:37 2018/6/25
 * @Description:
 */
public class ShapeFactory {

    public Shape getShape(String type) {
        if (type == null) {
            return null;
        } else if (type.equalsIgnoreCase("Rectangle")) {
            return new Rectangle();
        } else if (type.equalsIgnoreCase("Circle")) {
            return new Circle();
        } else if (type.equalsIgnoreCase("Shape")) {
            return new Square();
        }
        return null;
    }

    public static Object getClass(Class<?extends Shape> clazz) {
        Object obj = null;
        try {
            obj = Class.forName(clazz.getName()).newInstance();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        //反射得到方法
        Method draw = getClass(Circle.class).getClass().getMethod("draw", String.class);
        //获取方法属于哪个类
        Object o = getClass(Circle.class);
        //方法输出 args 参数
        draw.invoke(o,"hhhhhhhsdsd");
    }
}
